package cn.toleo.sso.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.toleo.common.pojo.ToleoResult;
import cn.toleo.pojo.TbUser;
import cn.toleo.sso.service.UserService;
import cn.toleo.utils.CookieUtils;
import cn.toleo.utils.JsonUtils;

/**
 * 用户处理controller
 * @author Administrator
 *
 */
@Controller
public class UserController {
	@Autowired
	private UserService userService;
//-------------------------------------------------------------------------------------	
	@RequestMapping("/user/check/{param}/{type}")
	@ResponseBody
	public ToleoResult checkUserData(@PathVariable String param,@PathVariable Integer type){
		
		//主干
		ToleoResult result = userService.checkData(param, type);
		
		//结果
		return result;
	}
	
    //-------------------------------------------------------------------------------------	
	@RequestMapping(value="/user/register", method=RequestMethod.POST)
	@ResponseBody
	public ToleoResult regitster(TbUser user){
		//主干
		ToleoResult result = userService.regidter(user);
		//结果
		return result;
	}

	//-------------------------------------------------------------------------------------
	
	@Value("${TOKEN_KEY}")
	private String TOKEN_KEY;
	@RequestMapping(value="/user/login", method=RequestMethod.POST)
	@ResponseBody
	public ToleoResult login(String username,String password, HttpServletResponse response,HttpServletRequest request){
		ToleoResult result = userService.login(username, password);
		//登录成功后写cookie
		if(result.getStatus()==200){
			CookieUtils.setCookie(request, response,TOKEN_KEY, result.getData().toString());
		}
		//结果
		return result;
	}
	
	//-------------------------------------------------------------------------------------
/*	@RequestMapping(value="user/token/{token}", method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public String getUserByToken(@PathVariable String token,String callback){
		//主干基础
		ToleoResult result = userService.getUserByToken(token);
		if(StringUtils.isNotBlank(callback)){
			return callback+"("+JsonUtils.objectToJson(result)+");";
		}
		//结果
		return JsonUtils.objectToJson(result);
	}*/
	@RequestMapping(value="user/token/{token}", method=RequestMethod.GET)
	@ResponseBody
	public Object getUserByToken(@PathVariable String token,String callback){
		//主干基础
		ToleoResult result = userService.getUserByToken(token);
		if(StringUtils.isNotBlank(callback)){
			MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(result);
			mappingJacksonValue.setJsonpFunction(callback);
			return mappingJacksonValue;
		}
		//结果
		return result;
	}
	
	
}
